var flag = false;
var div = false;
var username_used = false;
function validate() {
    if (valiemail() & valinickname() & valipassword1() & valipassword2() & checkusertype()) {
        var email = document.getElementById("txtEmail").value;
        var username = document.getElementById("txtNickName").value;
        var usertype = document.getElementById("usertype").value;
        var pwd = document.getElementById("txtPassword").value;
        var randCode = document.getElementById("validate_code").value;
        var url = window.location.href;
        var oldUrl = url;
        $.post(url,
            {
                email: encodeURIComponent(email),
                username: encodeURIComponent(username),
                usertype: encodeURIComponent(usertype),
                pwd: encodeURIComponent(pwd),
                randCode: encodeURIComponent(randCode)
            },
            function (data) {
                if (data == "ok") {
                    window.location = window.location.href + "/validate";
                } else if (data == "error_code") {
                    var number = document.getElementById("number_msg");
                    number.innerHTML = "*验证码错误";
                } else {
                    alert(data);
                    window.location.reload();
                }
            });
    } else {

    }

}
//验证邮箱
function valiemail() {
    var txtemail = document.getElementById("txtEmail");
    var txtemail_info = document.getElementById("email_info");
    var txtemail_info_ok = document.getElementById("email_info_ok");
    var reg = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;//正则邮箱验证
    txtemail_info.innerHTML = "";
    txtemail_info_ok.innerHTML = "";
    if (txtemail.value == "") {
        txtemail_info.innerHTML = "*邮箱不能为空";
        return false;
    } else if (!reg.test(txtemail.value)) {
        txtemail_info.innerHTML = "*邮箱格式不正确";
        return false;
    } else {
        var url = window.location.href + "/check_email";
        $.post(
            url,
            {email: $("#txtEmail").val()},
            function (data) {
                if (data == "-1") {
                    $("#email_info_ok").text("√邮箱可以使用");
                    div = true;
                } else {
                    $("#email_info").text("*邮箱已被占用");
                    div = false;
                }
            },
            "json"
        );
        if (div) {
            return true;
        } else {
            return false;
        }
    }
}
//验证用户名
function valinickname() {
    var txtNickName = document.getElementById("txtNickName");
    var txtNickName_info = document.getElementById("name.info");
    var txtNickName_info_ok = document.getElementById("name.info.ok");
    var reg = /[\u4E00-\u9FA5a-z0-9]{2,}/;
    txtNickName_info.innerHTML = "";
    txtNickName_info_ok.innerHTML = "";
    if (txtNickName.value == "") {
        txtNickName_info.innerHTML = "*昵称不能为空";
        return false;
    } else if (!reg.test(txtNickName.value) || txtNickName.value.replace(/[^\x00-\xff]/g, "xx").length < 4 || txtNickName.value.replace(/[^\x00-\xff]/g, "xx").length > 20) {
        txtNickName_info.innerHTML = "*昵称不符合要求";
        return false;
    } else {
        //验证用户名是否存在
        var username = document.getElementById("txtNickName").value;
        var url = window.location.href + "/check_username";
        $.post(url,
            {username: encodeURIComponent(username)},
            function (data) {
                if (data == "1") {
                    username_used = true;
                    txtNickName_info_ok.innerHTML = "√昵称符合要求"
                } else {
                    username_used = false
                    txtNickName_info.innerHTML = "*用户名已经占用";
                }
            },
            "json"
        );
        if (username_used) {
            return true;
        } else {
            return false;
        }
    }
}
//验证密码
function valipassword1() {
    var txtPassword = document.getElementById("txtPassword");
    var txtPassword_info = document.getElementById("password.info");
    var txtPassword_info_ok = document.getElementById("password.info.ok");
    var reg = /[\u4E00-\u9FA5A-Za-z0-9]{6,}/;
    txtPassword_info.innerHTML = "";
    txtPassword_info_ok.innerHTML = "";
    if (txtPassword.value == "") {
        txtPassword_info.innerHTML = "*密码不能为空";
        return false;
    } else if (!reg.test(txtPassword.value) || txtPassword.value.length > 20) {
        txtPassword_info.innerHTML = "*密码不符合要求";
        return false;
    } else {
        txtPassword_info_ok.innerHTML = "√密码符合要求";
        return true;
    }
}
//验证密码是否一致
function valipassword2() {
    var txtPassword = document.getElementById("txtPassword");
    var txtRepeatPass = document.getElementById("txtRepeatPass");
    var txtRepeatPass_info = document.getElementById("password1.info");
    var txtRepeatPass_info_ok = document.getElementById("password1.info.ok");
    txtRepeatPass_info.innerHTML = "";
    txtRepeatPass_info_ok.innerHTML = "";
    if (txtRepeatPass.value == "") {
        txtRepeatPass_info.innerHTML = "*密码确认不能为空";
        return false;
    } else if (txtRepeatPass.value != txtPassword.value) {
        txtRepeatPass_info.innerHTML = "*两次输入的密码不一致";
        return false;
    } else {
        txtRepeatPass_info_ok.innerHTML = "√两次输入密码一致";
        return true;
    }
}
function valicheckcode() {
    $("#number_msg").text("");
    $("#number_msg_ok").text("");
    if ($("#txtVerifyCode").val() == "") {
        $("#number_msg").text("*验证码为空");
        flag = false;
        return;
    }
    $.post(
        "checkcode.do",
        {code: $("#txtVerifyCode").val()},
        function (data) {
            if (data.ok) {
                $("#number_msg_ok").text("√验证码输入正确");
                flag = true;
            } else {
                $("#number_msg").text("*验证码不正确!");
                flag = false;
            }
        },
        "json"
    );
}

//检查用户是否已经选择用户类型
function checkusertype() {
    var flag = document.getElementById("usertype").value;
    if (flag == "") {
        $("#usertype_info").text("请您选择用户类型");
        return false;
    } else {
        $("#usertype_info").text("");
        return true;
    }
}

//刷新验证码
function changeValidateCode(obj) {
    // 获取当前的时间作为参数，无具体意义
    var timenow = new Date().getTime();
    // 每次请求需要一个不同的参数，否则可能会返回同样的验证码
    // 这和浏览器的缓存机制有关系，也可以把页面设置为不缓存，这样就不用这个参数了。
    obj.src = "../generateImage/u_v_i_c?d=" + timenow;
}